
package com.probiz.estore.sales.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.sales.RecommendedProduct;
import com.probiz.estore.common.model.sales.RecommendedType;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.util.StringUtil;
import com.probiz.estore.sales.SalesConstants;
import com.probiz.estore.sales.dao.RecommendedProductDao;
import com.probiz.estore.webapp.util.RequestContext;

public class RecommendedProductDaoImpl extends
		HibernateGenericDaoImpl<RecommendedProduct> implements
		RecommendedProductDao {

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.dao.RecommendedProductDao#getRecommendedProductsByRecommendedTypeIdBySourceId(int, java.lang.Integer, java.lang.Integer, int, int)
	 */
	public List<RecommendedProduct> getRecommendedProductsByRecommendedTypeIdBySourceId(final int recommendedTypeId, final Integer sourceKind,final Integer sourceId, final int firstResult, final int maxResults){
		List list;
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("from RecommendedProduct  where ");
				sb.append(" recommendedType.recommendedTypeId=" + recommendedTypeId);
				
				switch (sourceKind){
					case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCT: 
						sb.append(" and sourceProduct.productId=" + sourceId);break;
					case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCTCATEGORY: 
						sb.append(" and sourceCategory.categoryId=" + sourceId);break;
					default: break;
					
				}
				sb.append(" order by recommendedProductId desc ");
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(firstResult);
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<RecommendedProduct> rows = (List<RecommendedProduct>) query
						.list();
				
				return rows;
			}

		});
		return list;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.dao.RecommendedProductDao#getRecommendedProduct(java.lang.Integer, int, java.lang.Integer, java.lang.Integer)
	 */
	public RecommendedProduct getRecommendedProduct(Integer recommendedTypeId,int sourceKind,Integer sourceId, Integer productId){
		StringBuffer sb = new StringBuffer(200);
		sb.append("from RecommendedProduct where ");
		sb.append(" recommendedType.recommendedTypeId=" + recommendedTypeId);
		switch (sourceKind){
			case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCT: 
				sb.append(" and sourceProduct.productId=" + sourceId);break;
			case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCTCATEGORY: 
				sb.append(" and sourceCategory.categoryId=" + sourceId);break;
			default: break;
			
		}
		sb.append(" and product.productId=" + productId);
		sb.append(" order by recommendedProductId desc ");

		List list = getHibernateTemplate().find(sb.toString());
		if (list.size() > 0) {
			return (RecommendedProduct) list.get(0);
		} else {
			return null;
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.dao.RecommendedProductDao#getProductsByRecommendedTypeNameBySourceId(java.lang.String, int, java.lang.Integer, int, int)
	 */
	public List<Product> getProductsByRecommendedTypeNameBySourceId(final String recommendedTypeName, final int sourceKind, final Integer sourceId, final int firstResult, final int maxResults){
		List list;
		list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				StringBuffer sb = new StringBuffer(200);
				sb.append("select distinct(rp.product) from RecommendedProduct rp  where ");
				sb.append(" rp.recommendedType.typeName='"+ recommendedTypeName + "'");
				switch (sourceKind){
					case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCT: 
						sb.append(" and sourceProduct.productId=" + sourceId);break;
					case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCTCATEGORY: 
						sb.append(" and sourceCategory.categoryId=" + sourceId);break;
					default: break;
					
				}
				if(RequestContext.isFront()){
					//如果是前台获取，则必须取上架的
					sb.append(" and rp.product.status = 1  ");
				}
				sb.append(" order by rp.sortOrder asc ");
				Query query = session.createQuery(sb.toString());
				query.setFirstResult(firstResult);
				if (maxResults != -1)
					query.setMaxResults(maxResults);
				List<Product> rows = (List<Product>) query.list();
				 
				return rows;
			}

		});
		return list;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.dao.RecommendedProductDao#getProductsByRecommendedTypeNameBySourceIdList(java.lang.String, int, java.util.List, int, int)
	 */
	public List<Product> getProductsByRecommendedTypeNameBySourceIdList(final String recommendedTypeName, final int sourceKind, final List<Integer> sourceIds,	final int firstResult, final int maxResults){
		List list;
		if (sourceIds.size() != 0 ) {
			list = getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					StringBuffer sb = new StringBuffer(200);
					sb.append("select distinct(rp.product) from RecommendedProduct rp where ");
					sb.append(" rp.recommendedType.typeName='"+ recommendedTypeName + "'");
					String sourceIdsString = StringUtil.integerArrayToString(sourceIds.toArray(new Integer[0]), ",");
					switch (sourceKind){
						case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCT: 
							sb.append(" and rp.sourceProduct.productId in ( " + sourceIdsString + " )");break;
						case SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCTCATEGORY: 
							sb.append(" and rp.sourceCategory.categoryId in ( " + sourceIdsString + " )");break;
						default: break;
						
					}
					if(RequestContext.isFront()){
						//如果是前台获取，则必须取上架的
						sb.append(" and rp.product.status = 1  ");
					}
					sb.append(" order by rp.sortOrder asc ");
					Query query = session.createQuery(sb.toString());
					query.setFirstResult(firstResult);
					if (maxResults != -1)
						query.setMaxResults(maxResults);
					List<Product> rows = (List<Product>) query.list();
					return rows;
				}

			});
			return list;
		} else {
			return new ArrayList();
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.dao.RecommendedProductDao#getProductsByRecommendedTypeIdBySourceId(java.lang.Integer, int, java.lang.Integer, int, int)
	 */
	public List<Product> getProductsByRecommendedTypeIdBySourceId(final Integer recommendedTypeId, final int sourceKind,  final Integer sourceId,	final int firstResult, final int maxResults){
		RecommendedType recommendedType = (RecommendedType) getHibernateTemplate().get(RecommendedType.class, recommendedTypeId);
		if (null == recommendedType)		return new ArrayList<Product>();
		return this.getProductsByRecommendedTypeNameBySourceId(recommendedType.getTypeName(), sourceKind, sourceId,firstResult, maxResults);
	}
	
	

}